package com.jxs.android.weiget;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.telecom.weixi.R;

public class FilterViewContent_Common extends LinearLayout {

	private Button okBtn, cancelBtn;
	private BtnOnClickListern btnOnClickListern;

	private Button number_index, product_index, high_value_index, vip_club_index, add_stock_index;

	private List<Button> indexs = new ArrayList<Button>();

	private LinearLayout number_bar, product_bar, high_value_bar, vip_club_bar, add_stock_bar;
	private RadioGroup product_rg, high_value_rg, vip_club_rg, add_stock_rg, filter_sort_tag_rg;
	private EditText numberEdit;

	private String conditionProduct = "";// 查询条件-产品类型：全部(空),CDMA(7),固话(6),小灵通(3),宽带(2),其它(0)
	private String conditionHighValue = "";// 查询条件-11年锁定移动中高值用户:全部(空),是(1),否(0)
	private String conditionVipClub = "";// 查询条件-客户级别:全部(空),钻石卡(4),金卡(3),银卡(2),一般客户(1)
	private String conditionAddStock = "";// 查询条件-增存量标识:全部(空),存量(1),增量(2)
	private String conditionAccNbr = "";// 查询条件-手机号码：不限定号码时为空，限定号码需输入号码
	private String conditionSortTag = "";// 排序标示，1入网日期，2合账用户数，3账户ARPU，4账户余额

	private RadioButton conditionProduct_All, conditionHighValue_All, conditionVipClub_All, conditionAddStock_All, conditionSortTag_First;

	public FilterViewContent_Common(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	private void initNumber() {
		numberEdit = (EditText) findViewById(R.id.filter_number);
	}

	private void initFilterIndex() {
		number_index = (Button) findViewById(R.id.filter_number_index);
		product_index = (Button) findViewById(R.id.filter_product_index);
		high_value_index = (Button) findViewById(R.id.filter_high_value_index);
		vip_club_index = (Button) findViewById(R.id.filter_vip_club_index);
		add_stock_index = (Button) findViewById(R.id.filter_add_stock_index);

		number_index.setOnClickListener(indexBarListener);
		product_index.setOnClickListener(indexBarListener);
		high_value_index.setOnClickListener(indexBarListener);
		vip_club_index.setOnClickListener(indexBarListener);
		add_stock_index.setOnClickListener(indexBarListener);

		indexs.add(number_index);
		indexs.add(product_index);
		indexs.add(high_value_index);
		indexs.add(vip_club_index);
		indexs.add(add_stock_index);

		for (Button btn : indexs) {
			if (btn.getId() == number_index.getId()) {
				btn.setSelected(true);
			} else {
				btn.setSelected(false);
			}
		}

	}

	private Button.OnClickListener indexBarListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			if (v.getId() == number_index.getId()) {
				number_bar.setVisibility(View.VISIBLE);
				product_bar.setVisibility(View.GONE);
				high_value_bar.setVisibility(View.GONE);
				vip_club_bar.setVisibility(View.GONE);
				add_stock_bar.setVisibility(View.GONE);
			} else if (v.getId() == product_index.getId()) {
				number_bar.setVisibility(View.GONE);
				product_bar.setVisibility(View.VISIBLE);
				high_value_bar.setVisibility(View.GONE);
				vip_club_bar.setVisibility(View.GONE);
				add_stock_bar.setVisibility(View.GONE);
			} else if (v.getId() == high_value_index.getId()) {
				number_bar.setVisibility(View.GONE);
				product_bar.setVisibility(View.GONE);
				high_value_bar.setVisibility(View.VISIBLE);
				vip_club_bar.setVisibility(View.GONE);
				add_stock_bar.setVisibility(View.GONE);
			} else if (v.getId() == vip_club_index.getId()) {
				number_bar.setVisibility(View.GONE);
				product_bar.setVisibility(View.GONE);
				high_value_bar.setVisibility(View.GONE);
				vip_club_bar.setVisibility(View.VISIBLE);
				add_stock_bar.setVisibility(View.GONE);
			} else if (v.getId() == add_stock_index.getId()) {
				number_bar.setVisibility(View.GONE);
				product_bar.setVisibility(View.GONE);
				high_value_bar.setVisibility(View.GONE);
				vip_club_bar.setVisibility(View.GONE);
				add_stock_bar.setVisibility(View.VISIBLE);
			}

			for (Button btn : indexs) {
				if (btn.getId() == v.getId()) {
					btn.setSelected(true);
				} else {
					btn.setSelected(false);
				}
			}

		}
	};

	private void initFilterBar() {
		number_bar = (LinearLayout) findViewById(R.id.filter_number_bar);
		product_bar = (LinearLayout) findViewById(R.id.filter_product_bar);
		high_value_bar = (LinearLayout) findViewById(R.id.filter_high_value_bar);
		vip_club_bar = (LinearLayout) findViewById(R.id.filter_vip_club_bar);
		add_stock_bar = (LinearLayout) findViewById(R.id.filter_add_stock_bar);

		number_bar.setVisibility(View.VISIBLE);
		product_bar.setVisibility(View.GONE);
		high_value_bar.setVisibility(View.GONE);
		vip_club_bar.setVisibility(View.GONE);
		add_stock_bar.setVisibility(View.GONE);
	}

	private void initFilterRadioGroup() {
		product_rg = (RadioGroup) findViewById(R.id.filter_product_rg);
		high_value_rg = (RadioGroup) findViewById(R.id.filter_high_value_rg);
		vip_club_rg = (RadioGroup) findViewById(R.id.filter_vip_club_rg);
		add_stock_rg = (RadioGroup) findViewById(R.id.filter_add_stock_rg);
		filter_sort_tag_rg = (RadioGroup) findViewById(R.id.filter_sort_tag_rg);

		conditionProduct_All = (RadioButton) findViewById(R.id.filter_product_all);
		conditionHighValue_All = (RadioButton) findViewById(R.id.filter_high_value_all);
		conditionVipClub_All = (RadioButton) findViewById(R.id.filter_vip_club_all);
		conditionAddStock_All = (RadioButton) findViewById(R.id.filter_add_stock_all);
		conditionSortTag_First = (RadioButton) findViewById(R.id.filter_sort_tag_all);

		conditionProduct_All.setChecked(true);
		conditionHighValue_All.setChecked(true);
		conditionVipClub_All.setChecked(true);
		conditionAddStock_All.setChecked(true);
		conditionSortTag_First.setChecked(true);

		product_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
				Object v = radioGroup.findViewById(checkedId).getTag();
				conditionProduct = null == v ? "" : v.toString();
			}
		});
		high_value_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
				Object v = radioGroup.findViewById(checkedId).getTag();
				conditionHighValue = null == v ? "" : v.toString();
			}
		});
		vip_club_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
				Object v = radioGroup.findViewById(checkedId).getTag();
				conditionVipClub = null == v ? "" : v.toString();
			}
		});
		add_stock_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
				Object v = radioGroup.findViewById(checkedId).getTag();
				conditionAddStock = null == v ? "" : v.toString();
			}
		});
		filter_sort_tag_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
				Object v = radioGroup.findViewById(checkedId).getTag();
				conditionSortTag = null == v ? "" : v.toString();
			}
		});
	}

	private void initAlertDailogBtn() {
		okBtn = (Button) findViewById(R.id.filter_ok_btn);
		cancelBtn = (Button) findViewById(R.id.filter_cancel_btn);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();

		initAlertDailogBtn();
		initFilterBar();// 过滤器bar
		initFilterIndex();// 检索过滤器
		initNumber();
		initFilterRadioGroup();
	}

	public void setBtnOnClickListern(BtnOnClickListern btnListern) {
		this.btnOnClickListern = btnListern;
		okBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View paramView) {
				btnOnClickListern.okLogic();
			}
		});
		cancelBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View paramView) {
				btnOnClickListern.cancelLogic();
			}
		});
	}

	public String getConditionProduct() {
		return conditionProduct;
	}

	public String getConditionHighValue() {
		return conditionHighValue;
	}

	public String getConditionVipClub() {
		return conditionVipClub;
	}

	public String getConditionAddStock() {
		return conditionAddStock;
	}

	public String getConditionAccNbr() {
		conditionAccNbr = numberEdit.getText().toString();
		return conditionAccNbr;
	}

	public String getConditionSortTag() {
		return conditionSortTag;
	}

	static public interface BtnOnClickListern {
		public void okLogic();

		public void cancelLogic();
	}

}
